/** @description 框架提供的一些可在全局调用的核心方法 */
const jwt = require('jsonwebtoken');

function success(data, msg, code) {
  throw new global.helper.Success({data, msg, code});
}

function fail({errno, msg, code}) {
  throw new global.helper.Fail({errno, msg, code});
}

const generateToken = async (tokenInfo, expires = config.jwt.expires) => {
  return jwt.sign(tokenInfo, config.jwt.secret, {
    expiresIn:  expires// number of seconds
  });
};

const checkCaptcha = async (captchaId, value) => {
  const rv = await helper.redis.get(`verify:img:${captchaId}`);
  console.log('rv:',rv)
  console.log('value:',value)
  if (!rv || !value || value.toLowerCase() !== rv) {
    return false;
  } else {
    await helper.redis.del(`verify:img:${captchaId}`);
    return true;
  }
};

module.exports = {
  success
  , fail
  , generateToken
  , checkCaptcha
};
